package ui.comp;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import type.ITableLabelProviderObject;
import type.Variable;
import ui.custom.VariableTable;
import util.H2DB;

public class VariableComposite extends Composite {
	private VariableTable table;
	public VariableComposite(Composite parent, int style) {
		super(parent, style);
		List<ITableLabelProviderObject> accts = initData();
		this.setLayout(new FillLayout());
		Composite top = new Composite(this, SWT.NONE);
		top.setLayout(new GridLayout(1, true));
		table = new VariableTable(top, accts);
		if(accts.size()==1){
			Variable testData = (Variable) accts.get(0);
			if(testData.getId().equals("test")){
				table.clear();
			}
		}
	}

	private List<ITableLabelProviderObject> initData() {
		List<Variable> tmp = null;
		try {
			tmp = H2DB.getData(Variable.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<ITableLabelProviderObject> list = new ArrayList<ITableLabelProviderObject>();
		for(Variable mb:tmp){
			list.add(mb);
		}
		if(list.size()==0){
			list.add(new Variable("test","",""));
		}
		return list;
	}

	public VariableTable getTable() {
		return table;
	}
}
